package com.yz.android_baozou.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.yz.android_baozou.R;
import com.yz.android_baozou.adapter.ColumnDetialList_RecyclerAdapter;
import com.yz.android_baozou.bean.ColumnDetialListBean;
import com.yz.android_baozou.decoration.SpacesItemDecoration;
import com.yz.android_baozou.helper.OkhttpHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ColumnDetailList extends AppCompatActivity {

    @Bind(R.id.recyclerView_ColumnDetailList)
    RecyclerView recyclerViewColumnDetailList;
    @Bind(R.id.scoreView_ColumnDetailList)
    PullToRefreshScrollView scoreViewColumnDetailList;
    @Bind(R.id.toolbar_columndetiallist)
    Toolbar toolbarColumndetiallist;
    //声明上下文
    private Context mContext = this;
    //声明ColumnDetialList_RecyclerAdapter
    private ColumnDetialList_RecyclerAdapter recyclerAdapter;
    //声明实体类
    private ColumnDetialListBean columnDetialListBean;
    //声明实体类的集合
    private List<ColumnDetialListBean.DataBean> mList_ColumnDetailList = new ArrayList<>();
    //声明flag用来判断进行搜索还是下拉刷新
    private boolean flag;
    //初始化网址
    private String PATH_COLUMNDETIAL = "http://dailyapi.ibaozou.com/api/v31/channels/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_column_detaillist);
        ButterKnife.bind(this);
        //获得传入数据进行webView跳转
        Intent intent = getIntent();
        //声明bundle对象
        Bundle bundle = intent.getBundleExtra("id");
        //给bundle添加数据
        String id = bundle.getInt("id") + "";
        String title = bundle.getString("title");
        //设置toolbar的标题
        toolbarColumndetiallist.setTitle(title);
        //设置toolbar的头像
        toolbarColumndetiallist.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        //设置toolbar的监听
        toolbarColumndetiallist.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //更改网址
        PATH_COLUMNDETIAL = PATH_COLUMNDETIAL + id;
        //进行网络加载数据
        loadDataFromNet();
        //初始化数据
        initView();
    }

    private void loadDataFromNet() {
        //创建子线程 在子线程中进行耗时操作
        new Thread(new Runnable() {
            @Override
            public void run() {
                //获得解析的json串
                String json = OkhttpHelper.getStringFromUrl(PATH_COLUMNDETIAL);
                //通过Goson解析获得要解析的实体类
                columnDetialListBean = parseJsonToBean(json);
                //获得实体类的子集合
                mList_ColumnDetailList = columnDetialListBean.getData();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (flag) {
                            //在UI线程中进行适配器绑定
                            recyclerAdapter.reloadRecyclerView(mList_ColumnDetailList, true);
                        } else {
                            //在UI线程中进行适配器绑定
                            recyclerAdapter.reloadRecyclerView(mList_ColumnDetailList, false);
                        }
                        //取消下拉刷新
                        scoreViewColumnDetailList.onRefreshComplete();
                    }
                });
            }
        }).start();
    }

    private void initView() {
        //设置RecyclerView的中间的间距
        //使用线性布局管理器
        //设置RecyclerView的布局管理器
        //设置RecyclerView固定行高
        //创建适配器
        //设置RecyclerView的适配器
        recyclerViewColumnDetailList.addItemDecoration(new SpacesItemDecoration(10));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerViewColumnDetailList.setLayoutManager(manager);
        recyclerViewColumnDetailList.setHasFixedSize(true);
        recyclerAdapter = new ColumnDetialList_RecyclerAdapter(mContext, mList_ColumnDetailList, recyclerViewColumnDetailList);
        recyclerViewColumnDetailList.setAdapter(recyclerAdapter);
    }
    //  Gson解析网络数据为实体类
    public ColumnDetialListBean parseJsonToBean(String jsonString) {
        Gson gson = new Gson();
        return gson.fromJson(jsonString, new TypeToken<ColumnDetialListBean>() {
        }.getType());
    }
}
